#region License

// Copyright (c) 2008, BXO Solutions AB
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, 
//       this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright 
//       notice, this list of conditions and the following disclaimer in the 
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the BXO Solutions AB nor the names of its 
//       contributors may be used to endorse or promote products derived from 
//       this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Bxo.TestingPortal.Entities;
using Bxo.TestingPortal.WebApplication.Controllers;
using Castle.MonoRail.Framework;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Commons;
using Rhino.Commons.ForTesting;

namespace Bxo.TestingPortal.WebApplication.Tests {
    [TestFixture]
    public class ProjectControllerFixture : InProjectControllerTest {
        #region Setup/Teardown

        [SetUp]
        public void SetUp() {
            UnitOfWork.Start();
            DatabaseTestFixtureBase.CurrentContext.SetupDatabase(UnitOfWork.CurrentSession);

            this.testee = new ProjectController();
        }

        [TearDown]
        public void TearDown() {
            UnitOfWork.Current.Dispose();
        }

        #endregion

        [TestFixtureSetUp]
        public void TestFixtureSetUp() {
            DatabaseTestFixtureBase.IntializeNHibernateAndIoC(PersistenceFramework.ActiveRecord,
                                                              "Windsor.boo",
                                                              DatabaseEngine.SQLite,
                                                              MappingInfo.FromAssemblyContaining<Project>());
        }

        private ProjectController testee;

        protected override Controller Testee {
            get { return this.testee; }
        }

        [Test]
        public void Add_RendersExpectedView() {
            this.PrepareController(this.testee, "project", "add");

            this.testee.Add();

            Assert.That(this.testee.SelectedViewName, Is.EqualTo(@"project\add").IgnoreCase);
        }

        [Test]
        public void AddCommit_AddsProject() {
            this.PrepareController(this.testee, "project", "addcommit");

            this.testee.AddCommit("some_project");

            ICollection<Project> projects = Repository<Project>.FindAll();

            Assert.That(projects.Count, Is.EqualTo(1), "Controler must save exactly one project.");
            Project savedProject = projects.First();

            Assert.That(savedProject, Is.Not.Null);
            Assert.That(savedProject.Name, Is.EqualTo("some_project"));
            Assert.That(this.testee.PropertyBag["project"], Is.SameAs(savedProject));
            Assert.That(this.testee.SelectedViewName, Is.EqualTo(@"project\addcommit").IgnoreCase);
        }

        [Test]
        public void List_ShowsListOfProjects() {
            this.PrepareController(this.testee, "project", "list");

            using (new DataPreparationScope()) {
                var projects = new[] {new Project("proj1")};
                Array.ForEach(projects, project => Repository<Project>.Save(project));
            }

            this.testee.List(null);

            Assert.That(this.testee.PropertyBag["projects"], Is.EquivalentTo(Repository<Project>.FindAll().ToList()));
            Assert.That(this.testee.PropertyBag["project"], Is.Null);
            Assert.That(this.testee.SelectedViewName, Is.EqualTo(@"project\list").IgnoreCase);
        }

        [Test]
        public void List_ShowsListOfProjectsAndSetsProjectIfProjSpecified() {
            this.PrepareController(this.testee, "project", "list");

            int project2Id;

            using (new DataPreparationScope()) {
                var project1 = new Project("proj1") {Id = 1};
                var project2 = new Project("proj2") {Id = 2};
                var project3 = new Project("proj3") {Id = 3};
                var projects = new[] {project1, project2, project3};
                Array.ForEach(projects, project => Repository<Project>.Save(project));
                project2Id = 2;
            }

            this.testee.List(Repository<Project>.Get(project2Id));

            Assert.That(this.testee.PropertyBag["projects"], Is.EquivalentTo(Repository<Project>.FindAll().ToList()));
            Assert.That(this.testee.PropertyBag["project"], Is.SameAs(Repository<Project>.Get(project2Id)));
            Assert.That(this.testee.SelectedViewName, Is.EqualTo(@"project\list").IgnoreCase);
        }
    }
}